from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.views.decorators.clickjacking import xframe_options_exempt
from .forms.comment import CommentForm
from .models import Comment
from django.contrib.contenttypes.models import ContentType

# from notifications.signals import notify



# Create your views here.


# 心情评论
# @login_required(login_url='/userprofile/login/')
# 新增参数 parent_comment_id
@xframe_options_exempt  # 去掉这个view的响应头中的 X_FRAME_OPTIONS    解决frame 拒绝问题
def send_comment(request):
    # mood_id = request.POST.get('mood_id')
    # parent_comment_id = request.POST.get('parent_comment_id')
    # object_id = request.POST.get('object_id')
    # content_type = request.POST.get('content_type')


    # 处理 POST 请求
    if request.method == 'POST':

        comment_form = CommentForm(request.POST, user=request.universe.user)

        if comment_form.is_valid():

            # 检查通过，保存数据
            comment = Comment()
            comment.user = comment_form.cleaned_data['user']
            comment.body = comment_form.cleaned_data['body']
            # content_type = comment_form.cleaned_data['content_type']
            # object_id = comment_form.cleaned_data['object_id']
            comment.content_object = comment_form.cleaned_data['content_object']
            print('comment.content_object:{0}'.format(comment.content_object))
            parent = comment_form.cleaned_data['parent']

            # mood_object = get_object_or_404(content_type, id=object_id)

            # comment.save(commit=False)

            # 二级回复
            if parent:
                comment.parent = parent
                comment.reply_to = parent.user
                # parent_comment = MoodComment.objects.get(id=parent_comment_id)

                # 若回复层级超过二级，则转换为二级，get_root()获取root节点对象，parent_id设置为root的id
                comment.parent_id = parent.get_root().id

                # comment_form.instance.parent_id = parent_comment.get_root().id
                # 被回复人
                # comment_form.instance.reply_to = parent_comment.user
                # comment_form.save()

                # return JsonResponse({'status': True, "new_comment_id": comment_form.instance.id})

            comment.save()

            return JsonResponse({'status': True, "new_comment_id": comment.id})
        else:
            return HttpResponse("表单内容有误，请重新填写。")

    # 处理 GET 请求
    # elif request.method == 'GET':
    #     comment_form = MoodCommentForm()
    #     context = {
    #         'comment_form': comment_form,
    #         # 'mood_id': mood_id,
    #         # 'parent_comment_id': parent_comment_id
    #     }
    #     return render(request, 'comment/reply.html', context)
    # # 处理其他请求

    else:
        return HttpResponse("仅接受GET/POST请求。")

def delete_comment(request, comment_id):

    comment = Comment.objects.filter(id=comment_id, user=request.universe.user).first()
    if comment:
        url = comment.content_object.get_url()  # 拿到评论对象的实例，心情或文章实例，获取地址返回
        comment.delete()
        return redirect(url)
    else:
        JsonResponse({'status': False, 'data': '评论不存在'})